<?php
/**
* Do all the declarations etc needed by an admin page
*/
include_once "adminheader.inc";

echo "Adding extra option to database.<br />";

// the date fields are returned as arrays
// the cleanInput function purges arrays
// we need to save those fields (and clean them)
// manually
$dateFields = array (	'start_time',
						'end_time');

foreach ($dateFields as $key => $value) {
	// use intval to force strings to integers, this is done to
	// clean the input from the form
	$year = intval(substr($_POST[$value]['date'],0,4));
	$month = intval(substr($_POST[$value]['date'],5,2));
	$day = intval(substr($_POST[$value]['date'],8,2));
	
	$_POST[$value] = mktime(0, 0, 0, $month, $day, $year) + intval($_POST[$value]['time']);
}

$cleanPost = cleanInput($_POST);

// remove $ and , from money amounts
// this is US specific
$cleanPost['cost'] = str_replace(array('$',','),'',$cleanPost['cost']);
$cleanPost['guest_cost'] = str_replace(array('$',','),'',$cleanPost['guest_cost']);

// add the cleaned data to the database
$extra_insert = 'INSERT INTO ' . $xoopsDB->prefix('confreg_extras') .
				' SET name=' . $xoopsDB->quoteString(substr($cleanPost['name'],0, 40)) .
				', description=' . $xoopsDB->quoteString(substr($cleanPost['desc'],0,128)) .
				', cost=' . floatval($cleanPost['cost']) .
				', max_attend=' . intval($cleanPost['max_attend']) .
				', on_off=' . intval($cleanPost['on_off']) .
				', guests=' . intval($cleanPost['guests']) .
				', guest_cost=' . floatval($cleanPost['guest_cost']) .
				', start_date=FROM_UNIXTIME(' . intval($cleanPost['start_time']) . ')' .
				', end_date=FROM_UNIXTIME(' . intval($cleanPost['end_time']) . ')' .
				', conf_id=' . intval($cleanPost['conf_id']);

echo "Query built.<br />";


$insert_res = $xoopsDB->query($extra_insert);

if (!$insert_res) {
	echo "<p>Error occured inserting extra!";
} else {
	$extra_id = $xoopsDB->getInsertId();
	echo 'Extra option ' . $cleanPost['title'] . ' has been created. Extra id is ' . $extra_id . '<br />';
	echo '<p><form method="POST" action="addextras.php"><input type="hidden" name="conf_id" value="' . intval($cleanPost['conf_id']) . '" />';
	echo '<input type="submit" value="'. _AM_CONFREG_EXTRA_ADD_ANOTHER . '" /></form> <p> <a href="admenu0.php">Back to conference setup</a>';
}

xoops_cp_footer();

/*
 * This file was generated by XBS ModGen, (c) 2006 A Kitson, UK. See http://xoobs.net
 * ModGen is a Module Code Generator for the Xoops CMS.  See http://xoops.org
 */
?>
